package com.jiutong.meipengdai.views;

import android.animation.ArgbEvaluator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ProgressBar;

import com.jiutong.meipengdai.utils.DensityUtil;

/**
 *
 */
public class CircleProgressView extends ProgressBar {

    private final Context mContext;
    private Paint paint;//画笔
    private RectF oval; // RectF对象（圆环）
    private int currentDegree = 0;//当前度数（除360可求百分比）
    private ArgbEvaluator argbEvaluator = new ArgbEvaluator();//颜色渐变插值器
    private int height;//控件高
    private int width;//控件宽
    private int circleWidth;//圆环宽

    //渐变数组
    private int[] arcColors = new int[]{
            Color.parseColor("#ff6415"),
            Color.parseColor("#ff6415"),
            Color.parseColor("#ffc120"),
            Color.parseColor("#ffc120"),
            Color.parseColor("#ffc120"),
            Color.parseColor("#ff6415"),
            Color.parseColor("#ff6415")};

    private int strokeWidth = 20;//画笔大小
    private Paint paintCircle;
    private Paint paintPart;
    private float radius = 15;
    private float dx = 0; //阴影x轴偏移量
    private float dy = 7;//阴影y轴偏移量

    public CircleProgressView(Context context) {
        this(context, null, 0);
    }

    public CircleProgressView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // TODO Auto-generated constructor stub
        this.mContext = context;
        //初始化画笔
        paint = new Paint();
        paint.setAntiAlias(true);
        oval = new RectF();

        strokeWidth = DensityUtil.dip2px(context, 14);
        paint.setStrokeWidth(strokeWidth); // 线宽
        paint.setStyle(Paint.Style.STROKE);

        paintCircle = new Paint();
        paintCircle.setAntiAlias(true);
        paintCircle.setColor(Color.WHITE);

        //关闭硬件加速，要不然无法显示阴影
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
//        paintCircle.setShadowLayer(radius, dx, dy, 0xFF333333);

        paintPart = new Paint();
        paintPart.setAntiAlias(true);
        paintPart.setStrokeWidth(strokeWidth + 1); // 线宽 +1因为在某些手机上显示不完全覆盖下面
        paintPart.setStyle(Paint.Style.STROKE);
        paintPart.setStrokeCap(Paint.Cap.ROUND);
        paintPart.setColor(Color.parseColor("#e6e6e6"));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        //计算最小宽度
        height = MeasureSpec.getSize(heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);

        if (width >= height) {
            circleWidth = height;
        } else {
            circleWidth = width;
        }

        setMeasuredDimension(circleWidth, circleWidth);

        //一下操作相当于增加了间距为strokeWidth/2 的 padding
        oval.left = strokeWidth / 2; // 左边
        oval.top = strokeWidth / 2; // 上边
        oval.right = circleWidth - strokeWidth / 2; // 右边
        oval.bottom = circleWidth - strokeWidth / 2; // 下边
    }

    @SuppressLint("NewApi")
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);

        //设置颜色渐变
        SweepGradient sweepGradient =
                new SweepGradient(width / 2, height / 2, arcColors, null);

        Matrix matrix = new Matrix();
        matrix.setRotate(90);
        sweepGradient.setLocalMatrix(matrix);
        paint.setShader(sweepGradient);

        //draw外环
        canvas.drawArc(oval, 90, 360, false, paint);

//        if (currentDegree != 0) {
            //draw灰色部分
            canvas.drawArc(oval, 270, getProgress(), false, paintPart);
//        }

        //draw中间圆以及阴影
//        canvas.drawCircle(circleWidth / 2, circleWidth / 2, circleWidth / 2 - strokeWidth * 3, paintCircle);

    }

    /**
     * 根据百分比设置颜色范围
     *
     * @param pDegree 灰色区域所占百分比
     */
    public void setCurrentDegree(float pDegree) {
        this.currentDegree = (int) (360f * pDegree);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                paintCircle.setShadowLayer(radius, dx, dy, 0xFFF09738);//触摸时改变阴影颜色
                break;

            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                paintCircle.setShadowLayer(radius, dx, dy, 0xFF333333);
                break;
        }

        invalidate();
        return super.onTouchEvent(event);
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        super.setOnClickListener(l);
    }
}
